package com.ityu.aop;

import com.ityu.mapper.OperateLogMapper;
import com.ityu.pojo.Log.OperateLog;
import com.ityu.utlis.CurrentHolder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;

@Component
@Aspect
public class LogAspect {
    @Autowired
    OperateLogMapper operateLogMapper;
    @Around("execution(* com.ityu.controller.*.*(..)) &&  @annotation(com.ityu.anno.Log)")
    public Object RecordLog(ProceedingJoinPoint joinPoint) throws Throwable {
        long b = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long e = System.currentTimeMillis();


        OperateLog log = new OperateLog(
                null,
                CurrentHolder.getCurrentId(),
                LocalDateTime.now(),
                joinPoint.getTarget().getClass().getName(),
                joinPoint.getSignature().getName(),
                Arrays.toString(joinPoint.getArgs()),
                result.toString(),
                e-b
        );
        operateLogMapper.insert( log);
        return result;
    }

}
